/*
 * IndicatorControl.cpp
 *
 *  Created on: 2011-1-9
 *      Author: Administrator
 */

#include "IndicatorControl.h"

const TInt KButtonHeight = 48;

CIndicatorControl* CIndicatorControl::NewL(const TRect& aRect,const CCoeControl* aParent)
	{
	CIndicatorControl* self = CIndicatorControl::NewLC(aRect,aParent);
    CleanupStack::Pop(self);
    return self;	
	}

CIndicatorControl* CIndicatorControl::NewLC(const TRect& aRect,const CCoeControl* aParent)
	{
	CIndicatorControl* self = new(ELeave) CIndicatorControl();
    CleanupStack::PushL(self);
    self->ConstructL(aRect,aParent);
    return self;	
	}

CIndicatorControl::CIndicatorControl()
	{
	iLabelText = NULL;
	iCanCancel = EFalse;
	iCanSure = EFalse;
	iCancelRect.SetRect(0,0,0,0);
	iSureRect.SetRect(0,0,0,0);
	iObserver = NULL;
	}

CIndicatorControl::~CIndicatorControl()
	{
	delete iLabelText;
	iLabelText = NULL;
	}

void CIndicatorControl::SetLayout()
	{
	if (iImage)
		{
		iImageRect.SetSize(iImage->SizeInPixels());
		}
	
	TInt offset = 0;
	TInt xPos = Rect().iTl.iX;
	TInt yPos = (Rect().Height() - iImageRect.Height()) / 2 - 1;
	yPos += Font()->AscentInPixels();
	yPos += 18;
	
	if (iCanCancel && iCanSure)
		{
		offset = xPos + (Rect().Width() - 180) / 2;
		iSureRect.SetRect(offset,yPos,offset+80,yPos + KButtonHeight);
		iCancelRect.SetRect(offset + 100,yPos,offset+180,yPos + KButtonHeight);
		}
	else 
		{
		offset = xPos + (Rect().Width() - 100) / 2;
		if (iCanCancel)
			{
			iCancelRect.SetRect(offset,yPos,offset + 100,yPos + KButtonHeight);
			}
		if (iCanSure)
			{
			iSureRect.SetRect(offset,yPos,offset + 100,yPos + KButtonHeight);
			}
		}
	}

void CIndicatorControl::setLabelTextL(const TDesC& aText)
	{
    delete iLabelText;
    iLabelText = NULL;
    iLabelText = aText.AllocL();
	}

void CIndicatorControl::setCanCancel(TBool aCan)
	{
	iCanCancel = aCan;
	SetLayout();
	}

void CIndicatorControl::setCanSure(TBool aCan)
	{
	iCanSure = aCan;
	SetLayout();
	}

void CIndicatorControl::setIndicatorObserver(MIndicatorObserver* aObserver)
	{
	iObserver = aObserver;
	}

void CIndicatorControl::Draw(CWindowGc& gc, const TRect& aRect) const
	{
	gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
	gc.SetBrushColor(TRgb(0,0,0,128));
	gc.DrawRoundRect(aRect,TSize(5,5));
	
	TRect rect = iImageRect;
    TInt xPos = (Rect().Width() - rect.Width()) / 2;
    TInt yPos = (Rect().Height() - rect.Height()) / 2 - 1;
    
    if (iImage && iImage->Bitmap())
        {
        TPoint position = Position();
        TRect bmpPieceRect(TPoint(0, 0), iImage->SizeInPixels());
        if (iImage->MaskBitmap())
            gc.BitBltMasked(TPoint(position.iX + xPos, position.iY + yPos),iImage->Bitmap(),bmpPieceRect,iImage->MaskBitmap(),EFalse);
        else
            gc.BitBlt(TPoint(position.iX + xPos, position.iY + yPos), iImage->Bitmap(), bmpPieceRect);
        }
    if (iLabelText)
    	{
		gc.UseFont(Font());
    	gc.SetPenColor(KRgbWhite);
    	gc.SetBrushStyle(CGraphicsContext::ENullBrush);
    	TRect textRect(TPoint(aRect.iTl.iX,yPos + rect.Height() / 2 + 8), TPoint(aRect.iBr.iX, yPos + rect.Height() / 2 + 8 + Font()->AscentInPixels()));
    	TInt baseline = textRect.Height() / 2 + Font()->AscentInPixels() / 2;
    	gc.DrawText(*iLabelText, textRect, baseline, CGraphicsContext::ECenter);
    	gc.DiscardFont();
    	}
    if (iCanSure)
    	{
		gc.DrawRect(iSureRect);
		gc.UseFont(Font());
    	gc.SetPenColor(KRgbWhite);
    	gc.SetBrushStyle(CGraphicsContext::ENullBrush);
    	TInt baseline = iSureRect.Height() / 2 + Font()->AscentInPixels() / 2;
    	gc.DrawText(_L("Sure"), iSureRect, baseline, CGraphicsContext::ECenter);
    	gc.DiscardFont();
    	}
    if (iCanCancel)
    	{
		gc.DrawRect(iCancelRect);
		gc.UseFont(Font());
    	gc.SetPenColor(KRgbWhite);
    	gc.SetBrushStyle(CGraphicsContext::ENullBrush);
    	TInt baseline = iCancelRect.Height() / 2 + Font()->AscentInPixels() / 2;
    	gc.DrawText(_L("Cancel"), iCancelRect, baseline, CGraphicsContext::ECenter);
    	gc.DiscardFont();
    	}
	}

void CIndicatorControl::HandlePointerEventL(const TPointerEvent& aPointerEvent)
	{
	switch (aPointerEvent.iType)
		{
		case TPointerEvent::EButton1Up:
		case TPointerEvent::EButton2Up:
		case TPointerEvent::EButton3Up:
			if (iCanSure && iSureRect.Contains(aPointerEvent.iPosition))
				{
				if (iObserver)
					iObserver->HandleIndicatorEventL(1);
				MakeVisible(EFalse);
				}
			if (iCanCancel && iCancelRect.Contains(aPointerEvent.iPosition))
				{
				if (iObserver)
					iObserver->HandleIndicatorEventL(0);
				MakeVisible(EFalse);
				}
			break;
		default:
			break;
		}
	}
